#include "air001xx_hal.h"
#include "spi.h"
#include "key.h"
#include "uart.h"

#include "SoftTimer.h"
#include "AppLayer.h"
#include "ButtonLayer.h"
#include "xm_key.h"
#include "uart.h"
#include "WS2812_Control.h"



LongKey_Handle_Type stLongKey_Handle[KEY_NUM];


static void APP_SystemClockConfig(void); // RCC 时钟源配置和外设使能

int main(void)
{
	HAL_Init();				
	APP_SystemClockConfig();
	SPI_CONFIG();
	KEY_CONFIG();
	BSP_USART_Config();

#if 1
	printf("\n\nProject: colorfu light\n");
	printf("Version: V1.0\n");
	printf("Compile time: %s %s\n\n", __DATE__, __TIME__);
#endif	
	
	WS2812_Init(&ledStrip, &Spi1Handle, GPIOA, GPIO_PIN_7, LED_COUNT);

	//长按按键初始化----按键句柄--------------按键键值-----长按开始------长按保持---说明
	LongKeyEvents_Init(&stLongKey_Handle[0],KEY_MODE,1000/KEY_SCAN_TIME, 0);
	LongKeyEvents_Init(&stLongKey_Handle[1],KEY_H,1000/KEY_SCAN_TIME, 0);
	LongKeyEvents_Init(&stLongKey_Handle[2],KEY_V,1000/KEY_SCAN_TIME, 0);
	LongKeyEvents_Init(&stLongKey_Handle[3],KEY_R,1000/KEY_SCAN_TIME, 0);

	//长按按键事件注册----按键句柄-------------按下回调---松手回调---长按开始回调----长按保持回调-------------------说明
	LongKeyEvents_Set_CB(&stLongKey_Handle[0],NULL,ModeKey_ShortStart_CB,PowerKey_LongStart_CB,NULL);
	LongKeyEvents_Set_CB(&stLongKey_Handle[1],NULL,H_Key_Short_CB,NULL,H_Key_LongHold_CB);
	LongKeyEvents_Set_CB(&stLongKey_Handle[2],NULL,V_Key_Short_CB,NULL,V_Key_LongHold_CB);
	LongKeyEvents_Set_CB(&stLongKey_Handle[3],NULL,RST_Key_Short_CB,NULL,RST_Key_LongHold_CB);

	SoftTimer_Init();
	SoftTimer_Create(5,SoftwareTimer_XCScan_5ms);			// 5ms灯光色彩切换
	SoftTimer_Create(KEY_SCAN_TIME,Key_Analysis_50MS);
	// SoftTimer_Create(1000,SoftwareTimer_1000MS);
	printf("xcled_str.power_on = %d\n", xcled_str.power_on);
	HAL_UART_Receive_IT(&DebugUartHandle, &uart_rx_buffer[uart_rx_len], 1);
	
	while (1)
	{
		SoftTimer_PerformCallback();

	}
}

/*配置HSI16MHZ，SPI所在APB1,8MHZ*/
static void APP_SystemClockConfig(void)
{
	RCC_OscInitTypeDef RCC_OscInitStruct = {0};
	RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

	/* 配置HSI,HSE,LSE,LSI,PLL所有时钟 */
	RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
	RCC_OscInitStruct.HSIState = RCC_HSI_ON;						  /* 使能HSI */
	RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;						  /* HSI预分频 */
	RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_16MHz; /* 设置HSI输出时钟为16MHz,库会设置校准值 */
	RCC_OscInitStruct.HSEState = RCC_HSE_OFF;						  /* 禁止HSE */
	RCC_OscInitStruct.HSEFreq = RCC_HSE_16_32MHz;					  /* 设置HSE频率范围,没用可以不设置 */
	RCC_OscInitStruct.LSIState = RCC_LSI_OFF;						  /* 禁止LSI */
	RCC_OscInitStruct.LSEState = RCC_LSE_OFF;						  /* 禁止LSE */
	RCC_OscInitStruct.LSEDriver = RCC_LSEDRIVE_MEDIUM;				  /*  LSE默认驱动能力  */
	RCC_OscInitStruct.PLL.PLLState = RCC_PLL_OFF;					  /* 不用锁相环倍频PLL */
	RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;			  /* PLL的时钟源，PLL时钟源只有24M时, 再使能PLL，此处的定义没啥用*/

	if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) /* 配置时钟 */
	{
		Error_Handler();
	}

	/* 初始化AHB,APB总线时钟 */
	RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1;
	RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; /* 配置AHB时钟源 */
	RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;	   /* 设置AHB预分频 */
	RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;	   /* 设置APB1预分频,APB1不分频，在SPI配置中2分频 */

	if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) /* 配置总线 */
	{
		Error_Handler();
	}

	__HAL_RCC_FLASH_CLK_ENABLE(); /*flash使能*/
	__HAL_RCC_GPIOB_CLK_ENABLE(); /* 使能GPIOB时钟 */
	__HAL_RCC_GPIOA_CLK_ENABLE(); /* 使能GPIOA时钟 */
	__HAL_RCC_SPI1_CLK_ENABLE();  /*使能时钟 SPI1*/
}

void Error_Handler(void)
{
	while (1)
	{
	}
}


/**
  * @brief  USART发送回调执行函数
  * @param  huart：USART句柄
  * @retval 无
  */
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
{

}



